package com.alquimia.identityaccess.rest.controller;

import com.alquimia.identityaccess.application.events.user.*;
import com.alquimia.identityaccess.application.services.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.util.UriComponentsBuilder;

/**
 * Created by Ivo on 17/07/2014.
 */
@Controller
@RequestMapping("/users/{userId}")
public class UserCommandController {

    private static Logger LOG = LoggerFactory.getLogger(RegisterUserCommandController.class);

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/email", method = RequestMethod.PATCH)
    public ResponseEntity<UserEmailAddressUpdatedEvent> changeUserEmailAddress(@PathVariable String userId, @RequestBody UpdateEmailAddressEvent aUpdateEmailAddressEvent, UriComponentsBuilder builder) {

        UserEmailAddressUpdatedEvent userEmailAddressUpdatedEvent = userService.changeEmailAddress(aUpdateEmailAddressEvent);

        HttpHeaders headers = new HttpHeaders();
        headers.setLocation(
                builder.path("/users/{userId}")
                        .buildAndExpand(userEmailAddressUpdatedEvent.getUserId()).toUri()
        );

        return new ResponseEntity<UserEmailAddressUpdatedEvent>(userEmailAddressUpdatedEvent, headers, HttpStatus.OK);
    }

    @RequestMapping(value = "/name", method = RequestMethod.PATCH)
    public ResponseEntity<UserPersonalNameUpdatedEvent> changeUserPersonalName(@PathVariable String userId, @RequestBody UpdateUserPersonalNameEvent aUpdateUserPersonalNameEvent, UriComponentsBuilder builder) {

        UserPersonalNameUpdatedEvent userPersonalNameUpdatedEvent = userService.changeUserPersonalName(aUpdateUserPersonalNameEvent);

        HttpHeaders headers = new HttpHeaders();
        headers.setLocation(
                builder.path("/users/{userId}")
                        .buildAndExpand(userPersonalNameUpdatedEvent.getUserId()).toUri()
        );

        return new ResponseEntity<UserPersonalNameUpdatedEvent>(userPersonalNameUpdatedEvent, headers, HttpStatus.OK);
    }

    @RequestMapping(value = "/password", method = RequestMethod.PATCH)
    public ResponseEntity<UserPasswordUpdatedEvent> changeUserPassword(@PathVariable String userId, @RequestBody UpdateUserPasswordEvent aUpdateUserPasswordEvent, UriComponentsBuilder builder) {

        UserPasswordUpdatedEvent userPasswordUpdatedEvent = userService.changeUserPassword(aUpdateUserPasswordEvent);

        HttpHeaders headers = new HttpHeaders();
        headers.setLocation(
                builder.path("/users/{userId}")
                        .buildAndExpand(userPasswordUpdatedEvent.getUserId()).toUri()
        );

        return new ResponseEntity<UserPasswordUpdatedEvent>(userPasswordUpdatedEvent, headers, HttpStatus.OK);
    }
}
